package com.test.spcloud.controller;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.oauth2.common.OAuth2AccessToken;
//import org.springframework.security.oauth2.provider.OAuth2Authentication;
//import org.springframework.security.oauth2.provider.OAuth2Request;
//import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.test.spcloud.auth.User;
import com.test.spcloud.config.AuthConsts;
import com.test.spcloud.utils.JSONUtils;



@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private AuthorizationServerTokenServices authorizationServerTokenServices;
	
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public String registerSubmit() {
		String username = "123123123";
		String password = "111111";
		// login
		Map<String, String> parameters = new HashMap<String, String>();
		parameters.put("username", username);
		parameters.put("password", password);
		parameters.put("grant_type", "password");

		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		authorities.add(new SimpleGrantedAuthority(AuthConsts.ROLE_USER));

		Set<String> scopes = new HashSet<String>();
		scopes.add("read");
		scopes.add("write");

		OAuth2Request oauth2Request = new OAuth2Request(parameters, "acme", authorities, true, scopes, null, null, null,
				null);

		// Create principal and auth token
		UserDetails userPrincipal = User.create(username,password, AuthConsts.ROLE_USER);

		UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userPrincipal,
				null, authorities);

		OAuth2Authentication authenticationRequest = new OAuth2Authentication(oauth2Request, authenticationToken);
		authenticationRequest.setAuthenticated(true);
		OAuth2AccessToken oAuth2AccessToken = authorizationServerTokenServices.createAccessToken(authenticationRequest);
		return JSONUtils.Obj2Json(oAuth2AccessToken);

	}
}
